Copyright © Wei MEI, MLMS™—all rights reserved. 🀤
Collections¶
Collections are groups of items. Python supports several types of collections. Three of the most common are dictionaries, lists and arrays.
Lists¶
Lists are a collection of items. Lists can be expanded or contracted as needed, and can contain any data type. Lists are most commonly used to store a single column collection of information, however it is possible to nest lists
Arrays¶
Arrays are similar to lists, however are designed to store a uniform basic data type, such as integers or floating point numbers.
Dictionaries¶
Dictionaries are key/value pairs of a collection of items. Unlike a list where items can only be accessed by their index or value, dictionaries use keys to identify each item.
1 2 3 4 5 6 | names = ['Susan', 'Christopher', 'Bill']
presenters = names[0:2] # Get the first two items
# Starting index and number of items to retrieve
print(names)
print(presenters)
|
1 2 3 4 5 6 | names = ['Christopher', 'Susan']
scores = []
scores.append(98)
scores.append(99)
print(names)
print(scores)
|
Demo: dates¶
1 2 3 4 | person = {'first': 'Christopher'}
person['last'] = 'Harrison'
print(person)
print(person['first'])
|
1 2 3 4 | names = ['Christopher', 'Susan']
print(len(names)) # Get the number of items
names.insert(0, 'Bill') # Insert before index
print(names)
|
PPT Demonstrations¶
1 2 3 4 5 | from array import array
scores = array('d')
scores.append(97)
scores.append(98)
print(scores)
|